package ysu.sxm.ptjc.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.AccountInfoDao;
import ysu.sxm.ptjc.Dao.ProduceStatisticalInfoDao;
import ysu.sxm.ptjc.Dao.produceInfoDao;
import ysu.sxm.ptjc.Domin.DailyPassRate;
import ysu.sxm.ptjc.Domin.DateRange;
import ysu.sxm.ptjc.Domin.MonthQualified;
import ysu.sxm.ptjc.Domin.MonthQualifiedInfo;
import ysu.sxm.ptjc.Service.MonthQualifiedService;
import ysu.sxm.ptjc.Utill.Utills;
import ysu.sxm.ptjc.pojo.AccountInfo;
import ysu.sxm.ptjc.pojo.ProduceInfo;
import ysu.sxm.ptjc.pojo.ProduceStatisticalInfo;

import java.sql.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class MonthQualifiedServiceImpl implements MonthQualifiedService {

    Utills u;

    @Autowired
    ProduceStatisticalInfoDao produceStatisticalInfoDao;
    @Autowired
    AccountInfoDao accountInfoDao;
    @Autowired
    produceInfoDao proInfoDao;

    List<MonthQualifiedInfo> monthQualifiedInfos = new LinkedList<>();
    List<DailyPassRate> dailyPassRates = new LinkedList<>();

    @Override
    public List<MonthQualifiedInfo> week(MonthQualified monthQualified) {
        List<Integer> w = monthQualified.getWeek();
        List<DateRange> dateRanges = u.getWeekDays(w);
        List<String> produceTypes = monthQualified.getProduceType();
        for (String produceType:produceTypes) {
            for(DateRange dateRange:dateRanges){
               List<ProduceStatisticalInfo> produceStatisticalInfos = produceStatisticalInfoDao.getByProduceTypeAndDate(
                       produceType, (dateRange.getStart()),(dateRange.getEnd()));
               for (ProduceStatisticalInfo produceStatisticalInfo:produceStatisticalInfos) {
                   MonthQualifiedInfo m = new MonthQualifiedInfo();
                   m.setProduceType(produceStatisticalInfo.getProduceType());
                   m.setProduceDate(produceStatisticalInfo.getProduceDate());
                   m.setProduceQuantity(produceStatisticalInfo.getProduceQuantity());
                   m.setQualifyNum(produceStatisticalInfo.getQualifyNum());
                   Integer infoId = produceStatisticalInfo.getInfoId();
                   AccountInfo accountInfo = accountInfoDao.findAccountById(infoId);
                   m.setFirstpassRate(accountInfo.getFirstpassRate());
                   List<ProduceInfo> produceInfos = proInfoDao.getProduceInfoByTypeNumDate(Date.valueOf(dateRange.getStart()),
                           Date.valueOf(dateRange.getEnd()),produceStatisticalInfo.getProduceNum(),
                           produceStatisticalInfo.getProduceType());
                   for (ProduceInfo produceInfo:produceInfos) {
                       MonthQualifiedInfo m2 = new MonthQualifiedInfo();
                       m2 = m;
                       m2.setProduceName(produceInfo.getProduceName());
                       monthQualifiedInfos.add(m2);
                   }
               }
            }
        }
        return monthQualifiedInfos;
    }

    @Override
    public List<MonthQualifiedInfo> month(MonthQualified monthQualified) {
        List<Integer> w = monthQualified.getMonth();
        List<DateRange> dateRanges = u.getMonthDays(w);
        List<String> produceTypes = monthQualified.getProduceType();
        for (String produceType:produceTypes) {
            for(DateRange dateRange:dateRanges){
                List<ProduceStatisticalInfo> produceStatisticalInfos = produceStatisticalInfoDao.getByProduceTypeAndDate(
                        produceType, (dateRange.getStart()),(dateRange.getEnd()));
                for (ProduceStatisticalInfo produceStatisticalInfo:produceStatisticalInfos) {
                    MonthQualifiedInfo m = new MonthQualifiedInfo();
                    m.setProduceType(produceStatisticalInfo.getProduceType());
                    m.setProduceDate(produceStatisticalInfo.getProduceDate());
                    m.setProduceQuantity(produceStatisticalInfo.getProduceQuantity());
                    m.setQualifyNum(produceStatisticalInfo.getQualifyNum());
                    Integer infoId = produceStatisticalInfo.getInfoId();
                    AccountInfo accountInfo = accountInfoDao.findAccountById(infoId);
                    m.setFirstpassRate(accountInfo.getFirstpassRate());
                    List<ProduceInfo> produceInfos = proInfoDao.getProduceInfoByTypeNumDate(Date.valueOf(dateRange.getStart()),
                            Date.valueOf(dateRange.getEnd()),produceStatisticalInfo.getProduceNum(),
                            produceStatisticalInfo.getProduceType());
                    for (ProduceInfo produceInfo:produceInfos) {
                        MonthQualifiedInfo m2 = new MonthQualifiedInfo();
                        m2 = m;
                        m2.setProduceName(produceInfo.getProduceName());
                        monthQualifiedInfos.add(m2);
                    }
                }
            }
        }
        return monthQualifiedInfos;
    }

    @Override
    public List<DailyPassRate> dailypassrate(Date date) {
       List<ProduceInfo> produceInfos = proInfoDao.getProdeuceInfoByDate02(date);
        for (ProduceInfo produceInfo:produceInfos) {
            DailyPassRate dailyPassRate = new DailyPassRate();
            dailyPassRate.setProduceName(produceInfo.getProduceName());
            String produceNum = produceInfo.getProduceNum();
            ProduceStatisticalInfo produceStatisticalInfo = produceStatisticalInfoDao.getByProduceNumAndDate(produceNum,date);
            Integer infoId = produceStatisticalInfo.getInfoId();
            AccountInfo accountInfo = accountInfoDao.findAccountById(infoId);
            dailyPassRate.setFirstpassRate(accountInfo.getFirstpassRate());
            dailyPassRates.add(dailyPassRate);
        }
        return dailyPassRates;
    }
}
